package com.sky.controller.admin;

import com.github.pagehelper.Page;
import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.CategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Select;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/category")
@Slf4j
@Api(tags = "分类管理接口")
public class CategoryController {
    @Autowired
    CategoryService categoryService;

    /**
     * 分压查询
     * @param categoryPageQueryDTO
     * @return
     */
    @ApiOperation("分页查询")
    @GetMapping("/page")
    public Result<PageResult> page(CategoryPageQueryDTO categoryPageQueryDTO)
    {   PageResult pageResult=categoryService.page(categoryPageQueryDTO);
        return Result.success(pageResult);
    }

    @Transactional
    @ApiOperation("新增分类")
    @PostMapping
    public Result insert(@RequestBody CategoryDTO categoryDTO)
    {   log.info(String.valueOf(categoryDTO));
        categoryService.insert(categoryDTO);
        return Result.success();
    }

    @ApiOperation("修改分类")
    @PutMapping
    public Result update(@RequestBody CategoryDTO categoryDTO)
    {   categoryService.update(categoryDTO);
        return Result.success();
    }
    @PostMapping("/status/{status}")
    @ApiOperation("启用禁用")
    public Result startOrStop(@PathVariable Integer status,Long id)
    {
        categoryService.startOrStop(status,id);
        return Result.success();
    }

    @ApiOperation("根据ID删除分类")
    @DeleteMapping
    public Result deleteById(Long id)
    {
        categoryService.deleteById(id);
        return Result.success();
    }

    @ApiOperation("根据类型查询")
    @GetMapping("/list")
    public  Result list(Integer type)
    {
        List<Category> categories=categoryService.selectType(type);
        return Result.success(categories);
    }
}
